package edu.kit.ksri.db.mturk.utils;

import java.util.ArrayList;
import java.util.Collection;

import edu.kit.ksri.db.mturk.utils.numberevaluationhelpers.AbstractInterval;

/**
 * Copyright (c) 2010-2011 Karlsruhe Institute of Technology (KIT), Germany
 *
 * Permission is hereby granted, free of charge, to any person obtaining a
 * copy of this software and associated documentation files (the "Software"),
 * to deal in the Software without restriction, including without limitation
 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
 * and/or sell copies of the Software, and to permit persons to whom the
 * Software is furnished to do so, subject to the following conditions:
 * 
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
 * DEALINGS IN THE SOFTWARE.
 * 
 * --------------------------------------------------------------------------
 * 
 * Software: CSP/WMV tool for dynamic quality management of microtasks
 * http://www.ksri.kit.edu/Upload/Users/PeopleClouds/HCOMP2011_An_extendable_toolkit_for_managing_quality_of_human-based_eServices.pdf
 * 
 * Organization: Karlsruhe Institute of Technology (KIT), Germany
 * http://www.kit.edu
 * 
 * Project: PeopleClouds project of the Karlsruhe Service Research Institute(KSRI)
 * http://www.ksri.kit.edu/Default.aspx?PageId=818
 * 
 * Contributing authors: David Bermbach, Sandra Rath, Pascal Wichmann, Robert Kern
 */
public class NumberEvaluation {

	/** minimum value */
	private double min;

	/** maximum value */
	private double max;

	/** average value */
	private double avg;

	/** all raw values */
	private final ArrayList<Double> rawValues = new ArrayList<Double>();

	/**
	 * all intervals which shall be used for classes. May contain intervals
	 * which are not disjoint!
	 */
	private final ArrayList<AbstractInterval> intervals = new ArrayList<AbstractInterval>();

	/** stores the results of evaluation. */
	private String[][] occurencesByInterval = null;

	/**
	 * @param rawValues
	 *            all raw values
	 * @param intervals
	 *            all intervals which shall be used for classes. May contain
	 *            intervals which are not disjoint! Should be ordered, otherwise
	 *            output will not be ordered either
	 */
	public NumberEvaluation(ArrayList<Double> rawValues,
			ArrayList<AbstractInterval> intervals) {
		super();
		this.rawValues.addAll(rawValues);
		this.intervals.addAll(intervals);
	}

	/**
	 * @param rawValues
	 *            all raw values
	 * @param intervals
	 *            all intervals which shall be used for classes. May contain
	 *            intervals which are not disjoint! Should be ordered, otherwise
	 *            output will not be ordered either
	 */
	public NumberEvaluation(ArrayList<AbstractInterval> intervals,
			double... rawValues) {
		super();
		for (double d : rawValues)
			this.rawValues.add(d);
		this.intervals.addAll(intervals);
	}

	/**
	 * @param rawValues
	 *            all raw values
	 * @param intervals
	 *            all intervals which shall be used for classes. May contain
	 *            intervals which are not disjoint! Should be ordered, otherwise
	 *            output will not be ordered either
	 */
	public NumberEvaluation(AbstractInterval[] intervals, double... rawValues) {
		super();
		for (double d : rawValues)
			this.rawValues.add(d);
		for (AbstractInterval a : intervals)
			this.intervals.add(a);
	}

	public void evaluate() {
		max = MathUtils.max(rawValues.toArray(new Double[0]));
		min = MathUtils.min(rawValues.toArray(new Double[0]));
		avg = MathUtils.avg(rawValues.toArray(new Double[0]));
		occurencesByInterval = new String[2][intervals.size()];
		int[] temp = new int[intervals.size()];
		for (int i = 0; i < temp.length; i++)
			temp[i] = 0;
		int counter;
		for (double d : rawValues) {
			counter = 0;
			for (AbstractInterval a : intervals) {
				if (a.isInInterval(d))
					temp[counter]++;
				counter++;
			}

		}

		for (int i = 0; i < temp.length; i++) {
			occurencesByInterval[0][i] = "" + intervals.get(i);
			occurencesByInterval[1][i] = ("" + temp[i]).replace('.', ',');
		}
	}

	/**
	 *adds another interval
	 * 
	 * @param interval
	 */
	public void addInterval(AbstractInterval interval) {
		intervals.add(interval);
	}

	/**
	 * adds another value
	 * 
	 */
	public void addValue(double d) {
		rawValues.add(d);
	}

	/**
	 *adds intervals
	 * 
	 * @param interval
	 *            a collection of intervals
	 */
	public void addAllIntervals(Collection<? extends AbstractInterval> intervals) {
		this.intervals.addAll(intervals);
	}

	/**
	 * adds values
	 * 
	 * @param d
	 *            collection of values
	 * 
	 */
	public void addAllValues(Collection<Double> d) {
		rawValues.addAll(d);
	}

	/**
	 * @return the min
	 */
	public double getMin() {
		return this.min;
	}

	/**
	 * @return the max
	 */
	public double getMax() {
		return this.max;
	}

	/**
	 * @return the avg
	 */
	public double getAvg() {
		return this.avg;
	}

	/**
	 * @return the occurencesByInterval
	 */
	public String[][] getOccurencesByInterval() {
		return this.occurencesByInterval;
	}

	/**
	 * 
	 *all occurrences of '.' will be replaced with ','
	 * @return a CSV version of the results
	 */
	public String getResultsAsCSV() {
		String res = "RESULTS:;\n";
		AbstractInterval[] intervs = intervals
				.toArray(new AbstractInterval[intervals.size()]);
		res += CSVUtils.toCSV(occurencesByInterval);
		String[][] meanValues = new String[2][3];
		meanValues[0][0] = "Max";
		meanValues[0][1] = "Min";
		meanValues[0][2] = "Avg";
		meanValues[1][0] = ("" + max).replace('.', ',');
		meanValues[1][1] = ("" + min).replace('.', ',');
		meanValues[1][2] = ("" + avg).replace('.', ',');
		res += "\n\n" + CSVUtils.toCSV(meanValues);
		res += "\n\nRAW VALUES:;\n";
		String[] temp = CSVUtils.toStringArray(rawValues
				.toArray(new Double[rawValues.size()]));
		for(int i = 0; i<temp.length;i++){
			temp [i]= temp [i].replace('.', ',');
		}
		res += CSVUtils.toCSV(CSVUtils.oneDimToTwoDim(temp));

		return res;
	}

}
